<!--
 * @Author: 陆康永 email@email.com
 * @Date: 2024-11-13 14:10:47
 * @LastEditors: 陆康永 email@email.com
 * @LastEditTime: 2024-11-13 14:11:57
 * @FilePath: /数学/椭圆运动.html
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>div圆周运动</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 200px;
            height: 100px;
            border: 1px solid blue;
            border-radius: 100%;
            margin: 30px auto 0;
            position: relative;
        }

        .pointer {
            left: 0;
            top: 0;
            position: absolute;
            width: 10px;
            height: 10px;
            background-color: rebeccapurple;
            z-index: 100;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="pointer" id="pointer"></div>
    </div>
    <script>
        let div = document.querySelector('#pointer');
        let R = 100; // 长半轴
        let r = 50;  // 短半轴
        let angle = 0; // 开始角度

        function animation() {
            // 角度转弧度
            var radians = angle * (Math.PI / 180);
            // 计算椭圆上的点
            var x = R * Math.cos(radians);
            var y = r * Math.sin(radians);

            div.style.left = x + R + 'px';
            div.style.top = y + r + 'px';

            angle = angle + 1;
            requestAnimationFrame(animation);
        }

        animation();
    </script>
</body>

</html>